//进入页面，加载数据
$.ajax({
    url:"/teacher/findAllTeacher.do",
    type:"post",
    data:{

    },
    success: function(value){
        console.log(value)
        var data=value;
        $("tbody").empty();
        for(var i=0;i<7;i++){
            $("tbody").append(
                "<tr>"+
                "<td>"+data[i].id+"</td>"+
                "<td>"+data[i].name+"</td>"+
                "<td>"+data[i].sex+"</td>"+
                "<td>"+data[i].age+"</td>"+
                "<td>"+data[i].phone+"</td>"+
                "<td>"+data[i].create_time+"</td>"+
                "<td>"+
                "<input type='button' value='删除' class='delete' index='"+data[i].id+"'>"+
                "<input type='button' value='修改' class='update' index='"+data[i].id+"'>"+
                "<input type='button' value='查看详情' class='detail' index='"+data[i].id+"'>"+
                " </td>"+
                "</tr>"
            )
        }
    },
    error: function(){
        alert("请联系管理员！");
    }
})
//加载页码
$.ajax({
    url:"/teacher/findAllCount.do",
    type:"post",
    data:{

    },
    success: function(value){
        console.log(value)
        var data=value;
        $(".pageBox").empty();
        $(".pageBox").append(
            "<span class='first'>首页</span>"+
            "<span class='pre'>上一页</span>"
        )
        for(var i=0;i<Math.ceil(data/7);i++){
            if(i==0){
                $(".pageBox").append(
                    "<span class='page pageCurrent'>"+(i+1)+"</span>"
                )
            }else{
                $(".pageBox").append(
                    "<span class='page'>"+(i+1)+"</span>"
                )
            }

        }
        $(".pageBox").append(
            "<span class='next'>下一页</span>"+
            "<span class='last'>尾页</span>"
        )
    },
    error: function(){
        alert("请联系管理员！");
    }
})
function loadPage(){
    var name=$(".nameTest").val();
    if($("select").val()==1){
        var sex="";
    }else if($("select").val()==2){
        var sex="男";
    }else{
        var sex="女";
    }
    //加载页码
    $.ajax({
        url:"/teacher/findAllCountBySearch.do",
        type:"post",
        data:{
            sex:sex,
            name:name
        },
        success: function(value){
            console.log(value);
            var data=value;
            $(".pageBox").empty();
            $(".pageBox").append(
                "<span class='first'>首页</span>"+
                "<span class='pre'>上一页</span>"
            )
            for(var i=0;i<Math.ceil(data/7);i++){
                if(i==0){
                    $(".pageBox").append(
                        "<span class='pageCurrent'>"+(i+1)+"</span>"
                    )
                }else{
                    $(".pageBox").append(
                        "<span class='page'>"+(i+1)+"</span>"
                    )
                }

            }
            $(".pageBox").append(
                "<span class='next'>下一页</span>"+
                "<span class='last'>尾页</span>"
            )
        },
        error: function(){
            alert("请联系管理员！");
        }
    })
}
//加载数据
function loadData(page){
    var name=$(".nameTest").val();
    if($("select").val()=="1"){
        var sex="";
    }else if($("select").val()=="2"){
        var sex="男";
    }else{
        var sex="女";
    }
    var startDataPos=(page-1)*7;
    var endDataPos=page*7;
    $.ajax({
        url:"/teacher/findAllBySearch.do",
        type:"post",
        data:{
            sex:sex,
            name:name,
            startPage:startDataPos,
            endPage:endDataPos
        },
        success: function(value){
            console.log(value);
            var data=value;
            if(endDataPos>=value.length){
                $("tbody").empty();
                for(var i=startDataPos;i<value.length;i++){
                    $("tbody").append(
                        "<tr>"+
                        "<td>"+data[i].id+"</td>"+
                        "<td>"+data[i].name+"</td>"+
                        "<td>"+data[i].sex+"</td>"+
                        "<td>"+data[i].age+"</td>"+
                        "<td>"+data[i].phone+"</td>"+
                        "<td>"+data[i].create_time+"</td>"+
                        "<td>"+
                        "<input type='button' value='删除' class='delete' index='"+data[i].id+"'>"+
                        "<input type='button' value='修改' class='update' index='"+data[i].id+"'>"+
                        "<input type='button' value='查看详情' class='detail' index='"+data[i].id+"'>"+
                        " </td>"+
                        "</tr>"
                    )
                }
            }else{
                $("tbody").empty();
                for(var i=startDataPos;i<endDataPos;i++){
                    $("tbody").append(
                        "<tr>"+
                        "<td>"+data[i].id+"</td>"+
                        "<td>"+data[i].name+"</td>"+
                        "<td>"+data[i].sex+"</td>"+
                        "<td>"+data[i].age+"</td>"+
                        "<td>"+data[i].phone+"</td>"+
                        "<td>"+data[i].create_time+"</td>"+
                        "<td>"+
                        "<input type='button' value='删除' class='delete' index='"+data[i].id+"'>"+
                        "<input type='button' value='修改' class='update' index='"+data[i].id+"'>"+
                        "<input type='button' value='查看详情' class='detail' index='"+data[i].id+"'>"+
                        " </td>"+
                        "</tr>"
                    )
                }
            }

        },
        error: function(){
            alert("请联系管理员！");
        }
    })
}
$(".find").on("click",function(){
    loadPage();
    loadData(1);
})
$(".add").on("click",function(){
    location.href="/teacher/addPage.do";
})

$("tbody").on("click",".delete",function(){
    if(confirm("确定要删除吗？")){
        $.ajax({
            url:"/teacher/delete.do",
            data:{
                id:$(this).attr("index")
            },
            success:function(value){
                console.log(value)
                alert(value.msg);
                location.reload();
            },
            error:function (){
                alert("请联系管理员！")
            }
        })
    }
})
$("tbody").on("click",".detail",function(){
    $.cookie("teacherId",$(this).attr("index"));
    location.href="/teacher/lookOther.do"
})
$("tbody").on("click",".update",function(){
    $.cookie("teacherId",$(this).attr("index"));
    location.href="/teacher/updatePage.do";
})

//分页切换点击事件
$(".pageBox").on("click",".page",function(){
    //效果切换
    $(this).addClass("pageCurrent").siblings().removeClass("pageCurrent")
    //数据切换
    loadData($(this).text())
})
//首页切换点击事件
$(".pageBox").on("click",".first",function(){
    //效果切换
    $(".page").removeClass("pageCurrent").first().addClass("pageCurrent");
    //数据切换
    loadData(1);

})
//尾页切换点击事件
$(".pageBox").on("click",".last",function(){
    //效果切换
    $(".page").removeClass("pageCurrent").last().addClass("pageCurrent");
    //数据切换
    loadData($(".page").last().text());

})
//上一页切换点击事件
$(".pageBox").on("click",".pre",function(){
    //数据切换
    var prevPage=parseInt($(".pageBox .pageCurrent").text())-1;//当前页数的上一页
    if(prevPage<1){
        alert("已经是第一页！")
    }
    else{
        loadData(prevPage);
        $(".pageBox .pageCurrent").removeClass("pageCurrent").addClass("page").prev().addClass("pageCurrent");
    }
})
//下一页切换点击事件
$(".pageBox").on("click",".next",function(){
    //数据切换
    var nextPage=parseInt($(".pageBox .pageCurrent").text())+1;//当前页数的上一页
    if(nextPage>$(".page").last().text()){
        alert("已经是最后一页！")
    }
    else{
        loadData(nextPage);
        //效果切换
        $(".pageBox .pageCurrent").removeClass("pageCurrent").next().addClass("pageCurrent");
    }

})